<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>aria.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_a11y_aria_aria.js.html">aria.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Utilities for adding, removing and setting ARIA roles and
<a name="line18"></a> * states as defined by W3C ARIA standard: http://www.w3.org/TR/wai-aria/
<a name="line19"></a> * All modern browsers have some form of ARIA support, so no browser checks are
<a name="line20"></a> * performed when adding ARIA to components.
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.a11y.aria&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.a11y.aria.Role&#39;);
<a name="line27"></a>goog.require(&#39;goog.a11y.aria.State&#39;);
<a name="line28"></a>goog.require(&#39;goog.a11y.aria.datatables&#39;);
<a name="line29"></a>goog.require(&#39;goog.array&#39;);
<a name="line30"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line31"></a>goog.require(&#39;goog.dom&#39;);
<a name="line32"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line33"></a>goog.require(&#39;goog.object&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * ARIA states/properties prefix.
<a name="line38"></a> * @private
<a name="line39"></a> */
<a name="line40"></a>goog.a11y.aria.ARIA_PREFIX_ = &#39;aria-&#39;;
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * ARIA role attribute.
<a name="line45"></a> * @private
<a name="line46"></a> */
<a name="line47"></a>goog.a11y.aria.ROLE_ATTRIBUTE_ = &#39;role&#39;;
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * A list of tag names for which we don&#39;t need to set ARIA role and states
<a name="line52"></a> * because they have well supported semantics for screen readers or because
<a name="line53"></a> * they don&#39;t contain content to be made accessible.
<a name="line54"></a> * @private
<a name="line55"></a> */
<a name="line56"></a>goog.a11y.aria.TAGS_WITH_ASSUMED_ROLES_ = [
<a name="line57"></a>  goog.dom.TagName.A,
<a name="line58"></a>  goog.dom.TagName.AREA,
<a name="line59"></a>  goog.dom.TagName.BUTTON,
<a name="line60"></a>  goog.dom.TagName.HEAD,
<a name="line61"></a>  goog.dom.TagName.INPUT,
<a name="line62"></a>  goog.dom.TagName.LINK,
<a name="line63"></a>  goog.dom.TagName.MENU,
<a name="line64"></a>  goog.dom.TagName.META,
<a name="line65"></a>  goog.dom.TagName.OPTGROUP,
<a name="line66"></a>  goog.dom.TagName.OPTION,
<a name="line67"></a>  goog.dom.TagName.PROGRESS,
<a name="line68"></a>  goog.dom.TagName.STYLE,
<a name="line69"></a>  goog.dom.TagName.SELECT,
<a name="line70"></a>  goog.dom.TagName.SOURCE,
<a name="line71"></a>  goog.dom.TagName.TEXTAREA,
<a name="line72"></a>  goog.dom.TagName.TITLE,
<a name="line73"></a>  goog.dom.TagName.TRACK
<a name="line74"></a>];
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Sets the role of an element. If the roleName is
<a name="line79"></a> * empty string or null, the role for the element is removed.
<a name="line80"></a> * We encourage clients to call the goog.a11y.aria.removeRole
<a name="line81"></a> * method instead of setting null and empty string values.
<a name="line82"></a> * Special handling for this case is added to ensure
<a name="line83"></a> * backword compatibility with existing code.
<a name="line84"></a> *
<a name="line85"></a> * @param {!Element} element DOM node to set role of.
<a name="line86"></a> * @param {!goog.a11y.aria.Role|string} roleName role name(s).
<a name="line87"></a> */
<a name="line88"></a>goog.a11y.aria.setRole = function(element, roleName) {
<a name="line89"></a>  if (!roleName) {
<a name="line90"></a>    // Setting the ARIA role to empty string is not allowed
<a name="line91"></a>    // by the ARIA standard.
<a name="line92"></a>    goog.a11y.aria.removeRole(element);
<a name="line93"></a>  } else {
<a name="line94"></a>    if (goog.asserts.ENABLE_ASSERTS) {
<a name="line95"></a>      goog.asserts.assert(goog.object.containsValue(
<a name="line96"></a>          goog.a11y.aria.Role, roleName), &#39;No such ARIA role &#39; + roleName);
<a name="line97"></a>    }
<a name="line98"></a>    element.setAttribute(goog.a11y.aria.ROLE_ATTRIBUTE_, roleName);
<a name="line99"></a>  }
<a name="line100"></a>};
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * Gets role of an element.
<a name="line105"></a> * @param {!Element} element DOM element to get role of.
<a name="line106"></a> * @return {!goog.a11y.aria.Role} ARIA Role name.
<a name="line107"></a> */
<a name="line108"></a>goog.a11y.aria.getRole = function(element) {
<a name="line109"></a>  var role = element.getAttribute(goog.a11y.aria.ROLE_ATTRIBUTE_);
<a name="line110"></a>  return /** @type {goog.a11y.aria.Role} */ (role) || null;
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Removes role of an element.
<a name="line116"></a> * @param {!Element} element DOM element to remove the role from.
<a name="line117"></a> */
<a name="line118"></a>goog.a11y.aria.removeRole = function(element) {
<a name="line119"></a>  element.removeAttribute(goog.a11y.aria.ROLE_ATTRIBUTE_);
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Sets the state or property of an element.
<a name="line125"></a> * @param {!Element} element DOM node where we set state.
<a name="line126"></a> * @param {!(goog.a11y.aria.State|string)} stateName State attribute being set.
<a name="line127"></a> *     Automatically adds prefix &#39;aria-&#39; to the state name if the attribute is
<a name="line128"></a> *     not an extra attribute.
<a name="line129"></a> * @param {string|boolean|number|!goog.array.ArrayLike.&lt;string&gt;} value Value
<a name="line130"></a> * for the state attribute.
<a name="line131"></a> */
<a name="line132"></a>goog.a11y.aria.setState = function(element, stateName, value) {
<a name="line133"></a>  if (goog.isArrayLike(value)) {
<a name="line134"></a>    var array = /** @type {!goog.array.ArrayLike.&lt;string&gt;} */ (value);
<a name="line135"></a>    value = array.join(&#39; &#39;);
<a name="line136"></a>  }
<a name="line137"></a>  var attrStateName = goog.a11y.aria.getAriaAttributeName_(stateName);
<a name="line138"></a>  if (value === &#39;&#39; || value == undefined) {
<a name="line139"></a>    var defaultValueMap = goog.a11y.aria.datatables.getDefaultValuesMap();
<a name="line140"></a>    // Work around for browsers that don&#39;t properly support ARIA.
<a name="line141"></a>    // According to the ARIA W3C standard, user agents should allow
<a name="line142"></a>    // setting empty value which results in setting the default value
<a name="line143"></a>    // for the ARIA state if such exists. The exact text from the ARIA W3C
<a name="line144"></a>    // standard (http://www.w3.org/TR/wai-aria/states_and_properties):
<a name="line145"></a>    // &quot;When a value is indicated as the default, the user agent
<a name="line146"></a>    // MUST follow the behavior prescribed by this value when the state or
<a name="line147"></a>    // property is empty or undefined.&quot;
<a name="line148"></a>    // The defaultValueMap contains the default values for the ARIA states
<a name="line149"></a>    // and has as a key the goog.a11y.aria.State constant for the state.
<a name="line150"></a>    if (stateName in defaultValueMap) {
<a name="line151"></a>      element.setAttribute(attrStateName, defaultValueMap[stateName]);
<a name="line152"></a>    } else {
<a name="line153"></a>      element.removeAttribute(attrStateName);
<a name="line154"></a>    }
<a name="line155"></a>  } else {
<a name="line156"></a>    element.setAttribute(attrStateName, value);
<a name="line157"></a>  }
<a name="line158"></a>};
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/**
<a name="line162"></a> * Remove the state or property for the element.
<a name="line163"></a> * @param {!Element} element DOM node where we set state.
<a name="line164"></a> * @param {!goog.a11y.aria.State} stateName State name.
<a name="line165"></a> */
<a name="line166"></a>goog.a11y.aria.removeState = function(element, stateName) {
<a name="line167"></a>  element.removeAttribute(goog.a11y.aria.getAriaAttributeName_(stateName));
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Gets value of specified state or property.
<a name="line173"></a> * @param {!Element} element DOM node to get state from.
<a name="line174"></a> * @param {!goog.a11y.aria.State|string} stateName State name.
<a name="line175"></a> * @return {string} Value of the state attribute.
<a name="line176"></a> */
<a name="line177"></a>goog.a11y.aria.getState = function(element, stateName) {
<a name="line178"></a>  // TODO(user): return properly typed value result --
<a name="line179"></a>  // boolean, number, string, null. We should be able to chain
<a name="line180"></a>  // getState(...) and setState(...) methods.
<a name="line181"></a>
<a name="line182"></a>  var attr =
<a name="line183"></a>      /** @type {string|number|boolean} */ (element.getAttribute(
<a name="line184"></a>      goog.a11y.aria.getAriaAttributeName_(stateName)));
<a name="line185"></a>  var isNullOrUndefined = attr == null || attr == undefined;
<a name="line186"></a>  return isNullOrUndefined ? &#39;&#39; : String(attr);
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Returns the activedescendant element for the input element by
<a name="line192"></a> * using the activedescendant ARIA property of the given element.
<a name="line193"></a> * @param {!Element} element DOM node to get activedescendant
<a name="line194"></a> *     element for.
<a name="line195"></a> * @return {?Element} DOM node of the activedescendant, if found.
<a name="line196"></a> */
<a name="line197"></a>goog.a11y.aria.getActiveDescendant = function(element) {
<a name="line198"></a>  var id = goog.a11y.aria.getState(
<a name="line199"></a>      element, goog.a11y.aria.State.ACTIVEDESCENDANT);
<a name="line200"></a>  return goog.dom.getOwnerDocument(element).getElementById(id);
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * Sets the activedescendant ARIA property value for an element.
<a name="line206"></a> * If the activeElement is not null, it should have an id set.
<a name="line207"></a> * @param {!Element} element DOM node to set activedescendant ARIA property to.
<a name="line208"></a> * @param {?Element} activeElement DOM node being set as activedescendant.
<a name="line209"></a> */
<a name="line210"></a>goog.a11y.aria.setActiveDescendant = function(element, activeElement) {
<a name="line211"></a>  var id = &#39;&#39;;
<a name="line212"></a>  if (activeElement) {
<a name="line213"></a>    id = activeElement.id;
<a name="line214"></a>    goog.asserts.assert(id, &#39;The active element should have an id.&#39;);
<a name="line215"></a>  }
<a name="line216"></a>
<a name="line217"></a>  goog.a11y.aria.setState(element, goog.a11y.aria.State.ACTIVEDESCENDANT, id);
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Gets the label of the given element.
<a name="line223"></a> * @param {!Element} element DOM node to get label from.
<a name="line224"></a> * @return {string} label The label.
<a name="line225"></a> */
<a name="line226"></a>goog.a11y.aria.getLabel = function(element) {
<a name="line227"></a>  return goog.a11y.aria.getState(element, goog.a11y.aria.State.LABEL);
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Sets the label of the given element.
<a name="line233"></a> * @param {!Element} element DOM node to set label to.
<a name="line234"></a> * @param {string} label The label to set.
<a name="line235"></a> */
<a name="line236"></a>goog.a11y.aria.setLabel = function(element, label) {
<a name="line237"></a>  goog.a11y.aria.setState(element, goog.a11y.aria.State.LABEL, label);
<a name="line238"></a>};
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>/**
<a name="line242"></a> * Asserts that the element has a role set if it&#39;s not an HTML element whose
<a name="line243"></a> * semantics is well supported by most screen readers.
<a name="line244"></a> * Only to be used internally by the ARIA library in goog.a11y.aria.*.
<a name="line245"></a> * @param {!Element} element The element to assert an ARIA role set.
<a name="line246"></a> * @param {!goog.array.ArrayLike.&lt;string&gt;} allowedRoles The child roles of
<a name="line247"></a> * the roles.
<a name="line248"></a> */
<a name="line249"></a>goog.a11y.aria.assertRoleIsSetInternalUtil = function(element, allowedRoles) {
<a name="line250"></a>  if (goog.array.contains(goog.a11y.aria.TAGS_WITH_ASSUMED_ROLES_,
<a name="line251"></a>      element.tagName)) {
<a name="line252"></a>    return;
<a name="line253"></a>  }
<a name="line254"></a>  var elementRole = /** @type {string}*/ (goog.a11y.aria.getRole(element));
<a name="line255"></a>  goog.asserts.assert(elementRole != null,
<a name="line256"></a>      &#39;The element ARIA role cannot be null.&#39;);
<a name="line257"></a>
<a name="line258"></a>  goog.asserts.assert(goog.array.contains(allowedRoles, elementRole),
<a name="line259"></a>      &#39;Non existing or incorrect role set for element.&#39; +
<a name="line260"></a>      &#39;The role set is &quot;&#39; + elementRole +
<a name="line261"></a>      &#39;&quot;. The role should be any of &quot;&#39; + allowedRoles +
<a name="line262"></a>      &#39;&quot;. Check the ARIA specification for more details &#39; +
<a name="line263"></a>      &#39;http://www.w3.org/TR/wai-aria/roles.&#39;);
<a name="line264"></a>};
<a name="line265"></a>
<a name="line266"></a>
<a name="line267"></a>/**
<a name="line268"></a> * Gets the boolean value of an ARIA state/property.
<a name="line269"></a> * @param {!Element} element The element to get the ARIA state for.
<a name="line270"></a> * @param {!goog.a11y.aria.State|string} stateName the ARIA state name.
<a name="line271"></a> * @return {?boolean} Boolean value for the ARIA state value or null if
<a name="line272"></a> *     the state value is not &#39;true&#39;, not &#39;false&#39;, or not set.
<a name="line273"></a> */
<a name="line274"></a>goog.a11y.aria.getStateBoolean = function(element, stateName) {
<a name="line275"></a>  var attr =
<a name="line276"></a>      /** @type {string|boolean} */ (element.getAttribute(
<a name="line277"></a>          goog.a11y.aria.getAriaAttributeName_(stateName)));
<a name="line278"></a>  goog.asserts.assert(
<a name="line279"></a>      goog.isBoolean(attr) || attr == null || attr == &#39;true&#39; ||
<a name="line280"></a>          attr == &#39;false&#39;);
<a name="line281"></a>  if (attr == null) {
<a name="line282"></a>    return attr;
<a name="line283"></a>  }
<a name="line284"></a>  return goog.isBoolean(attr) ? attr : attr == &#39;true&#39;;
<a name="line285"></a>};
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>/**
<a name="line289"></a> * Gets the number value of an ARIA state/property.
<a name="line290"></a> * @param {!Element} element The element to get the ARIA state for.
<a name="line291"></a> * @param {!goog.a11y.aria.State|string} stateName the ARIA state name.
<a name="line292"></a> * @return {?number} Number value for the ARIA state value or null if
<a name="line293"></a> *     the state value is not a number or not set.
<a name="line294"></a> */
<a name="line295"></a>goog.a11y.aria.getStateNumber = function(element, stateName) {
<a name="line296"></a>  var attr =
<a name="line297"></a>      /** @type {string|number} */ (element.getAttribute(
<a name="line298"></a>          goog.a11y.aria.getAriaAttributeName_(stateName)));
<a name="line299"></a>  goog.asserts.assert((attr == null || !isNaN(Number(attr))) &amp;&amp;
<a name="line300"></a>      !goog.isBoolean(attr));
<a name="line301"></a>  return attr == null ? null : Number(attr);
<a name="line302"></a>};
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * Gets the string value of an ARIA state/property.
<a name="line307"></a> * @param {!Element} element The element to get the ARIA state for.
<a name="line308"></a> * @param {!goog.a11y.aria.State|string} stateName the ARIA state name.
<a name="line309"></a> * @return {?string} String value for the ARIA state value or null if
<a name="line310"></a> *     the state value is empty string or not set.
<a name="line311"></a> */
<a name="line312"></a>goog.a11y.aria.getStateString = function(element, stateName) {
<a name="line313"></a>  var attr = element.getAttribute(
<a name="line314"></a>      goog.a11y.aria.getAriaAttributeName_(stateName));
<a name="line315"></a>  goog.asserts.assert((attr == null || goog.isString(attr)) &amp;&amp;
<a name="line316"></a>      isNaN(Number(attr)) &amp;&amp; attr != &#39;true&#39; &amp;&amp; attr != &#39;false&#39;);
<a name="line317"></a>  return attr == null ? null : attr;
<a name="line318"></a>};
<a name="line319"></a>
<a name="line320"></a>
<a name="line321"></a>/**
<a name="line322"></a> * Gets array of strings value of the specified state or
<a name="line323"></a> * property for the element.
<a name="line324"></a> * Only to be used internally by the ARIA library in goog.a11y.aria.*.
<a name="line325"></a> * @param {!Element} element DOM node to get state from.
<a name="line326"></a> * @param {!goog.a11y.aria.State} stateName State name.
<a name="line327"></a> * @return {!goog.array.ArrayLike.&lt;string&gt;} string Array
<a name="line328"></a> *     value of the state attribute.
<a name="line329"></a> */
<a name="line330"></a>goog.a11y.aria.getStringArrayStateInternalUtil = function(element, stateName) {
<a name="line331"></a>  var attrValue = element.getAttribute(
<a name="line332"></a>      goog.a11y.aria.getAriaAttributeName_(stateName));
<a name="line333"></a>  return goog.a11y.aria.splitStringOnWhitespace_(attrValue);
<a name="line334"></a>};
<a name="line335"></a>
<a name="line336"></a>
<a name="line337"></a>/**
<a name="line338"></a> * Splits the input stringValue on whitespace.
<a name="line339"></a> * @param {string} stringValue The value of the string to split.
<a name="line340"></a> * @return {!goog.array.ArrayLike.&lt;string&gt;} string Array
<a name="line341"></a> *     value as result of the split.
<a name="line342"></a> * @private
<a name="line343"></a> */
<a name="line344"></a>goog.a11y.aria.splitStringOnWhitespace_ = function(stringValue) {
<a name="line345"></a>  return stringValue ? stringValue.split(/\s+/) : [];
<a name="line346"></a>};
<a name="line347"></a>
<a name="line348"></a>
<a name="line349"></a>/**
<a name="line350"></a> * Adds the &#39;aria-&#39; prefix to ariaName.
<a name="line351"></a> * @param {string} ariaName ARIA state/property name.
<a name="line352"></a> * @private
<a name="line353"></a> * @return {string} The ARIA attribute name with added &#39;aria-&#39; prefix.
<a name="line354"></a> * @throws {Error} If no such attribute exists.
<a name="line355"></a> */
<a name="line356"></a>goog.a11y.aria.getAriaAttributeName_ = function(ariaName) {
<a name="line357"></a>  if (goog.asserts.ENABLE_ASSERTS) {
<a name="line358"></a>    goog.asserts.assert(ariaName, &#39;ARIA attribute cannot be empty.&#39;);
<a name="line359"></a>    goog.asserts.assert(goog.object.containsValue(
<a name="line360"></a>        goog.a11y.aria.State, ariaName),
<a name="line361"></a>        &#39;No such ARIA attribute &#39; + ariaName);
<a name="line362"></a>  }
<a name="line363"></a>  return goog.a11y.aria.ARIA_PREFIX_ + ariaName;
<a name="line364"></a>};
</pre>


</body>
</html>
